package worker

import (
	"cron-go/internal/pkg/crontab/dao"
	"cron-go/internal/pkg/crontab/enum"
	"cron-go/internal/pkg/crontab/models"
	"cron-go/internal/pkg/utils/shell"
	"fmt"

	"github.com/go-liam/util/conv"
)

// shell 工作者

func ShellWork(t *models.TaskMysqlModel) bool {
	if t.Command == "" {
		return false
	}
	r := shell.ExeSysCommand(t.Command)
	status := enum.DoStatusFail
	if r != "" {
		status = enum.DoStatusSuccess
	}
	//add log
	ml := new(models.TaskLogMysqlModel)
	ml.TaskID = t.ID
	ml.DoStatus = status
	result := conv.StringLeft(r, 200)
	ml.Info = fmt.Sprintf(`{"type":"shell","result":"%v"}`, result)
	new(dao.LogDao).Create(ml)
	//finish
	FinishWorker(t, status)
	return true
}
